﻿using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using SMCL.Core.Utils.Types.Json;

namespace SMCL.Core.Utils.Types
{
    public class GameInfo
    {
        [JsonInclude]
        public string GameArg { get; set; }
        [JsonInclude]
        public string JavaArg { get; set; }
        [JsonInclude]
        public int JavaVersion { get; set; }
        [JsonInclude]
        public GameInfo_Downloads Downloads { get; set; }
        [JsonInclude]
        public GameInfo_AssetIndex AssetIndex { get; set; }
        [JsonInclude]
        public GameInfo_Library[] Libraries { get; set; }
        [JsonInclude]
        public string MainClass { get; set; }
        [JsonInclude]
        public DateTime ReleaseTime { get; set; }
        [JsonInclude]
        public DateTime UpdateTime { get; set; }
        [JsonInclude]
        public Type Type { get; set; }
        [JsonInclude]
        public string VersionName { get; set; }
        public GameInfo(Json.GameInfo gameInfo, string versionName)
        {
            if (gameInfo.MinecraftArguments is not null)
            {
                GameArg = gameInfo.MinecraftArguments;
                JavaArg = "";
            }
            else
            {
                var sb = new StringBuilder();
                foreach (var i in gameInfo.Arguments.Game)
                    if (i.GetValueKind() == JsonValueKind.String)
                        sb.Append((string?)i + " ");
                GameArg = sb.ToString();
                sb.Clear();
                foreach (var i in gameInfo.Arguments.Jvm)
                    if (i.GetValueKind() == JsonValueKind.String)
                        sb.Append((string?)i + " ");
                    else if (i.GetValueKind() == JsonValueKind.Object)
                    {
                        Console.WriteLine(i.GetValueKind());
                        var jvmarg = i.Deserialize<GameInfo_Arguments_Jvm>();
                        if (jvmarg is not null && Array.Exists(jvmarg.Rules, rule => rule.Action == "allow" ? rule?.OS?.Arch == "x86" || rule?.OS?.Name == "windows" : rule?.OS?.Arch != "x86" || rule?.OS?.Name != "windows"))
                            if (jvmarg.Value.GetValueKind() == JsonValueKind.String)
                                sb.Append((string?)jvmarg.Value + " ");
                            else if (jvmarg.Value.GetValueKind() == JsonValueKind.Array)
                                foreach (var j in jvmarg.Value.AsArray().ToArray())
                                    if (j.GetValueKind() == JsonValueKind.String)
                                        sb.Append((string?)j + " ");
                    }
                //no logs
                //sb.Append(gameInfo.Logging.Client.Argument);
                JavaArg = sb.ToString();
            }
            AssetIndex = gameInfo.AssetIndex;
            JavaVersion = gameInfo.JavaVersion.MajorVersion;
            Downloads = gameInfo.Downloads;
            Libraries = (from library in gameInfo.Libraries where library.Rules is null || Array.Exists(library.Rules, rule => rule.Action == "allow" ? rule?.OS?.Arch == "x86" || rule?.OS?.Name == "windows" : rule?.OS?.Arch != "x86" || rule?.OS?.Name != "windows") select library).ToArray();
            MainClass = gameInfo.MainClass;
            ReleaseTime = gameInfo.ReleaseTime;
            UpdateTime = gameInfo.Time;
            Type = gameInfo.Type switch
            {
                "release" => Type.Release,
                "snapshot" => Type.Snapshot,
                "old_beta" => Type.Old,
                "old_alpha" => Type.Old,
                _ => throw new ArgumentException("未知的版本类型")
            };
            VersionName = versionName;
        }
    }
}
